/**
 * @ViewDrawSwap.as
 * 
 * @author sodaChen mail:asframe@163.com
 * @version 1.0
 * <br>Copyright (C), 2012 ASFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame AStruts2
 * <br>Date:2012-6-23
 */
package com.asframe.astruts2.controller
{
	import com.asframe.astruts2.beans.ViewBean;
	import com.asframe.astruts2.context.AStrutsData;
	import com.asframe.astruts2.interfaces.IViewHandler;
	import com.asframe.collections.maps.HashMap;
	import com.asframe.collections.maps.IMap;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * view对象的拖动和深度交换
	 * @author sodaChen
	 * @Date:2012-6-23
	 */
	public class ViewDrawSwap implements IViewHandler
	{
		/** 系统数据对象 **/
		private var astrutsData			:AStrutsData
		/** view的容器 **/
		private var container			:Sprite;
		/** 所有组的集合 **/
		private var allGroupMap			:HashMap;
		/** viewBen的集合(Sprite,ViewBean) **/
		private var viewBeanMap			:HashMap;
		/** 当前打开的界面容器 **/
		private var currentView			:Sprite;
		/** 计时器，防止拖动的误操作 **/
		private var timer				:Timer;
		
		
		public function ViewDrawSwap()
		{
			allGroupMap = new HashMap();
			viewBeanMap = new HashMap();
			//计时器的作用是防止误拖动
			timer = new Timer(10,1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerEvent,false,0,true);
		}
		/**
		 * @inheritDoc
		 */
		public function setAStrutsData(astrutsData:AStrutsData):void
		{
			this.astrutsData = astrutsData;
			container = astrutsData.root.getViewContainer();
		}
		/**
		 * @inheritDoc
		 */		
		public function addView(viewBean:ViewBean):void
		{
			viewBeanMap.put(viewBean.viewContainer,viewBean);
			viewBean.viewContainer.addEventListener(Event.ADDED_TO_STAGE,addStageEvent,false,0,true);
			viewBean.viewContainer.addEventListener(Event.REMOVED_FROM_STAGE,removeStageEvent,false,0,true);
		}
		/**
		 * @inheritDoc
		 */		
		public function removeView(viewBean:ViewBean):void
		{
			//删除view
			viewBean.viewContainer.removeEventListener(Event.ADDED_TO_STAGE,addStageEvent);
			viewBean.viewContainer.removeEventListener(Event.REMOVED_FROM_STAGE,removeStageEvent);
			removeViewStage(viewBean);
			viewBeanMap.remove(viewBean.name);
		}
		/**
		 * view从舞台上删除时调用的方法
		 * @param viewBean
		 */		
		private function removeViewStage(viewBean:ViewBean):void
		{
//			viewBeanMap.remove(viewBean.viewContainer);
			var groupMap:HashMap = getGroupMap(viewBean.group);
			//根据名称放到通道集合中
			groupMap.remove(viewBean.viewContainer);
			var viewContainer:Sprite = viewBean.viewContainer;
			if(viewBean.swap)
			{
				//删除交换事件
				viewContainer.removeEventListener(MouseEvent.CLICK,viewClickEvent);
			}
			if(viewBean.drag)
			{
				//删除拖动事件
				viewContainer.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownEvent);
				viewContainer.removeEventListener(MouseEvent.MOUSE_UP,mouseUpEvent);
			}
		}
		/**
		 * view添加到舞台后调用的方法 
		 * @param viewBean
		 * 
		 */		
		private function addViewStage(viewBean:ViewBean):void
		{
			//所有的的view一定会有个组的，至少会有个默认组
			var groupMap:HashMap = getGroupMap(viewBean.group);
			var viewSize:int = groupMap.size();
			var viewContainer:Sprite = viewBean.viewContainer;
			//查看是否独占,如果是独占，则把组里的其他界面都进行删除
			if(viewBean.alone && groupMap.size() > 0)
			{
				removeViews(groupMap.valuesToArray());
			}
			//删除互斥的views
			if(viewBean.mutexViewAry != null && viewBean.mutexViewAry.length > 0)
			{
				removeViews(viewBean.mutexViewAry);
			}
			//删除互斥的viewGroups
			var mutexGroupLen:int = 0;
			if(viewBean.mutexGroupAry != null && (mutexGroupLen = viewBean.mutexGroupAry.length) > 0)
			{
				for (var i:int = 0; i < mutexGroupLen; i++) 
				{
					var mutexGroupMap:IMap = getGroupMap(viewBean.mutexGroupAry[i]);
					if(mutexGroupMap != null && mutexGroupMap.size() > 0)
					{
						removeViews(mutexGroupMap.valuesToArray());
					}
				}
			}
			if(!viewBean.tab)
			{
				viewContainer.tabEnabled = false;
				viewContainer.tabChildren = false;
			}
			if(viewBean.swap)
			{
				viewContainer.addEventListener(MouseEvent.MOUSE_DOWN,viewClickEvent,false,0,true);
				//同时界面位置最顶层
				container.addChild(viewContainer);
			}
			if(viewBean.drag)
			{
				//添加拖动功能
				viewContainer.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownEvent,false,0,true);
				viewContainer.addEventListener(MouseEvent.MOUSE_UP,mouseUpEvent,false,0,true);
			}
			//根据名称放到通道集合中
			groupMap.put(viewContainer,viewBean);
		}
		/**
		 * view添加到舞台的时间
		 * @param event
		 */		
		private function addStageEvent(event:Event):void
		{
			var viewBean:ViewBean = viewBeanMap.get(event.target);
			addViewStage(viewBean);
		}
		/**
		 * view移除舞台事件
		 * @param event
		 */		
		private function removeStageEvent(event:Event):void
		{
			var viewBean:ViewBean = viewBeanMap.get(event.target);
			removeViewStage(viewBean);
		}
		/**
		 * 根据通道名称获取当前通道的所有view 
		 * @param group
		 * @return 
		 */		
		private function getGroupMap(group:String):HashMap
		{
			var map:HashMap = allGroupMap.get(group) as HashMap;
			if(map == null)
			{
				map = new HashMap();
				allGroupMap.put(group,map);
			}
			return map;
		}
		/**
		 * 移除view集合里面的全部view容器 
		 * @param viewMap
		 * @param viewSize
		 */		
		private function removeViews(views:Array):void
		{
			var viewSize:int = views.length;
			for (var i:int = 0; i < viewSize; i++) 
			{
				if(views[i].viewContainer.parent == container)
				{
					container.removeChild(views[i].viewContainer);
				}
			}
		}
		//view的点击事件
		private function viewClickEvent(event:MouseEvent):void
		{
			var viewBean:ViewBean = viewBeanMap.get(event.currentTarget);
			if(viewBean != null)
			{
				if(viewBean.swap)
				{
					//取出目前的顶层view对象
					var topView:Sprite = container.getChildAt(container.numChildren - 1) as Sprite;
					//和最顶层的view进行深度交换
					if(topView != null && topView != viewBean.view && viewBean.viewContainer.parent == container)
					{
						container.swapChildren(viewBean.viewContainer,topView);
					}
				}
			}
		}
		private function mouseDownEvent(event:MouseEvent):void
		{
			var viewBean:ViewBean = viewBeanMap.get(event.currentTarget) as ViewBean;
			if(viewBean.drag)
			{
				currentView = viewBean.viewContainer;
				timer.start();
			}
		}
		private function timerEvent(event:TimerEvent):void
		{
			timer.stop();
			timer.reset();
			if(currentView != null)
				currentView.startDrag();
		}
		private function mouseUpEvent(event:MouseEvent):void
		{
			timer.stop();
			timer.reset();
			if(currentView != null)
				currentView.stopDrag();
		}
	}
}
